package com.hevery.security.core.authentication.mobile;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.Collection;

/**
 * @author: zhy
 * @description: 自定义短信验证码的Authentication
 * @date: created on 2017/11/4
 * @modified by:
 */
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken{

    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    /**用于存放认证信息。在认证之前放手机号，在认证之后放登录成功之后的用户信息*/
    private final Object principal;


    /**
     * 未登录之前，存放手机号，setAuthenticated设置为false
     * @param mobile
     */
    public SmsCodeAuthenticationToken(String mobile) {
        super(null);
        this.principal = mobile;
        super.setAuthenticated(false);
    }

    /**
     * 登录成功之后，存放用户信息。setAuthenticated设置为true
     * @param principal 用户信息
     * @param authorities 用户的权限
     */
    public SmsCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return null;
    }

    @Override
    public void setAuthenticated(boolean authenticated) {
        super.setAuthenticated(authenticated);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
